package com.fowo.api.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.R;
import com.fowo.api.sys.entity.SysDict;
import com.fowo.api.sys.entity.SysDictItem;
import com.fowo.api.sys.model.*;
import com.fowo.api.sys.service.SysDictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 系统字典控制器
 * @author yl_ls
 */
@Tag(name = "系统字典控制器")
@RestController
@RequestMapping("api/sys/dict")
public class SysDictController {
    @Resource
    private SysDictService service;

    @Operation(summary = "分页查询")
    @GetMapping("page")
    public R<Page<SysDictItemVo>> pageSearch(SysDictSearchParamPo searchParamPo) {
        return R.data(() -> service.pageSearch(searchParamPo));
    }

    @Operation(summary = "新增")
    @PostMapping
    public R<?> create(@RequestBody @Validated CreateSysDictPo createSysDictPo) {
        return R.ok(() -> service.create(createSysDictPo));
    }

    @Operation(summary = "更新")
    @PutMapping
    public R<?> update(@RequestBody SysDict updateSysDict) {
        return R.ok(() -> service.update(updateSysDict));
    }

    @Operation(summary = "删除")
    @DeleteMapping("{ids}")
    public R<?> delete(@PathVariable String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return R.ok(() -> service.delete(idList));
    }

    @Operation(summary = "子项分页")
    @GetMapping("items/page")
    public R<Page<SysDictItem>> itemPageSearch(SysDictItemSearchParamPo searchParamPo) {
        return R.data(service.itemPageSearch(searchParamPo));
    }

    @Operation(summary = "获取全部可用子项")
    @GetMapping("items")
    public R<List<SysDictAllItem>> getAllItems() {
        return R.data(service.getAllItems());
    }

    @Operation(summary = "新增子项")
    @PostMapping("items")
    public R<?> createItem(@RequestBody @Validated CreateSysDictItemPo createSysDictItemPo) {
        return R.ok(() -> service.createItem(createSysDictItemPo));
    }

    @Operation(summary = "更新子项")
    @PutMapping("items")
    public R<?> updateItem(@RequestBody SysDictItem updateSysDictItem) {
        return R.ok(() -> service.updateItem(updateSysDictItem));
    }

    @Operation(summary = "删除项")
    @DeleteMapping("items/{ids}")
    public R<?> deleteItem(@PathVariable String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return R.ok(() -> service.deleteItem(idList));
    }
}
